package editAir.lightScene
{
	import _Pan3D.display3D.Display3DSprite;
	
	import flash.display.BitmapData;
	import flash.display3D.Context3D;
	import flash.display3D.Context3DProgramType;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.Context3DVertexBufferFormat;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.Program3D;
	import flash.display3D.VertexBuffer3D;
	import flash.display3D.textures.Texture;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class PerlinDisplay3DSprite extends Display3DSprite
	{
		private var timeNum:Number=1;
		private var context3D:Context3D;
		private var vertexBuffer:VertexBuffer3D;
		private var indexBuffer:IndexBuffer3D; 
		private var texture:Texture;
		public var newprogram:Program3D;
		public function PerlinDisplay3DSprite(context:Context3D)
		{
			super(context);
			context3D=context;
			makeMianpianModel();
			makeMianpianText();
		}
		
		private function makeMianpianText():void
		{
			var bmd:BitmapData=new BitmapData(256,256,true,0x000000);
			var periodX:Number=256;
			var periodY:Number=256;
			var numOctaves:Number=5;
			var seed:Number=0x00fff;
			var offsets:Array=new Array();
			for (var i:int = 0; i<=numOctaves-1; i++) {
				offsets.push(new Point());
			}
			bmd.perlinNoise(periodX,periodY,numOctaves,seed,true,true,1,true,offsets);
			
			var cTransform:ColorTransform = new ColorTransform();
			cTransform.alphaMultiplier = 0.7
			var rect:Rectangle = new Rectangle(0, 0, 256, 256);
			bmd.colorTransform(rect, cTransform);
			
			texture=context3D.createTexture(256,256, Context3DTextureFormat.BGRA, true);
			
			texture.uploadFromBitmapData(bmd);
	
			
		}
		override public function update() : void {
			if (!this._visible) {
				return;
			}
			if (_objData && _objData.texture) {
				_context.setProgram(newprogram);
				setVc();
				setVa();
				resetVa();
			}
		}
		override protected function setVa() : void {
			
			//context3D.setTextureAt(1, _objData.texture);
			context3D.setTextureAt(1, texture);
			context3D.setVertexBufferAt(0, vertexBuffer, 0, Context3DVertexBufferFormat.FLOAT_3);
			context3D.setVertexBufferAt(1, vertexBuffer, 3, Context3DVertexBufferFormat.FLOAT_3);
			context3D.drawTriangles(indexBuffer, 0, -1);
		}
		private function makeMianpianModel() : void {
			
			var speed : Number = 500;
			var m: Number=2;
			vertexBuffer  = context3D.createVertexBuffer(4, 6);
			vertexBuffer.uploadFromVector(Vector.<Number>(
				[-speed,  0,speed, 0, 0, 1,
					speed, 0,speed, 1/m, 0, 1, 
					speed, 0,-speed, 1/m, 1/m, 1, 
					-speed,0,-speed,  0, 1/m, 1,]), 0, 4);
			var k : Array = [0, 2, 3, 0, 1, 2];
			indexBuffer  = context3D.createIndexBuffer(k.length);
			indexBuffer.uploadFromVector(Vector.<uint>(k), 0, k.length);
		}
		override protected function resetVa() : void {
			_context.setVertexBufferAt(0, null);
			_context.setVertexBufferAt(1, null);
			context3D.setTextureAt(1, null);
		}
		
		override protected function setVc() : void {
			this.updateMatrix();
			if(_timer>200){
				
				_timer=0;
			}
			_timer++;
			_context.setProgramConstantsFromMatrix(Context3DProgramType.VERTEX, 4, modelMatrix, true);
			_context.setProgramConstantsFromVector(Context3DProgramType.FRAGMENT, 0, Vector.<Number>([_timer/200, 1, 1,1]));
			
		}
		private var _total:int=0;
		private var _skip:int=1
		private var _timer:int=0;
		
	}
}